/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import accessor.Customer;
import accessor.CustomerAccessor;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import tools.EmailValidator;

@WebServlet(name = "update_customer", urlPatterns = {"/update_customer"})
public class update_customer extends BaseServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        CustomerAccessor accessor = new CustomerAccessor();
        Customer customer = new Customer();
        int customerID = Integer.parseInt(request.getParameter("customerID"));
        String first_name = request.getParameter("first_name")!=null?request.getParameter("first_name"):"";
        String last_name = request.getParameter("last_name")!=null?request.getParameter("last_name"):"";
        String company = request.getParameter("company")!=null?request.getParameter("company"):"";
        String address = request.getParameter("address")!=null?request.getParameter("address"):"";
        String phone = request.getParameter("phone")!=null?request.getParameter("phone"):"";
        try {
            if (request.getParameter("submit") == null) {
                request.getSession().removeAttribute("updateCusError");
                response.sendRedirect("admin/update_customer.jsp?customerID=" + customerID);
            } else {
                int errorList = 0;
                if (first_name.equals("")) {
                    request.getSession().setAttribute("update_customer_firstname","<p>You must enter your first name!</p>");
                    errorList ++;
                }
                if (last_name.equals("")) {
                    request.getSession().setAttribute("update_customer_lastname","<p>You must enter your last name!</p>");
                    errorList ++;
                }
                if (address.equals("")) {
                    request.getSession().setAttribute("update_customer_lastname","<p>You must enter your address!</p>");
                    errorList ++;
                }
                if (phone.equals("")) {
                    request.getSession().setAttribute("update_customer_firstname","<p>You must enter your phone!</p>");
                    errorList ++;
                }


                if (errorList ==0) {
                    customer.setCustomerID(customerID);
                    customer.setFirstName(first_name);
                    customer.setLastName(last_name);
                    customer.setCompany(company);
                    customer.setAddress(address);
                    customer.setPhone(phone);

                    int retVal = 0;
                    retVal = accessor.updateCustomerProdile(customer);
                    if (retVal == 1) {
                        request.getSession().removeAttribute("updateCusError");
                        request.getSession().setAttribute("mess_customer", "Update successful!");
                        response.sendRedirect("admin/list_customer.jsp");
                    } else {
                        request.getSession().setAttribute("updateCusError", "Update Fail! Try again!");
                        response.sendRedirect("admin/update_customer.jsp?userID=" + customerID);
                    }
                } else {
                    response.sendRedirect("admin/update_customer.jsp?userID=" + customerID);
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        } finally {
            out.close();
        }
    }

    protected List<String> validateForm() {
        List<String> error = null;

        return error;
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
